﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class apps_ahmidea_pages_Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AHMIDEAIdentity id = (AHMIDEAIdentity)User.Identity;
        username.InnerHtml = "Welcome " + id.DisplayName + " (" + id.Nrp + ")";

        string user = AHMIDEAFunctions.ContextUser(HttpContext.Current);
        StringBuilder sbSavToday = new StringBuilder();
        StringBuilder sbSavYesterday = new StringBuilder();
        StringBuilder sbSavOlder = new StringBuilder();
        Dictionary<string, List<string>> arFav = new Dictionary<string, List<string>>();

        using (DC_AHMIDEADataContext dc = new DC_AHMIDEADataContext())
        {
            var res = dc.SPAHMIDEA_GetSavedData(user);

            foreach (var item in res)
            {
                if (item.itype == 2)
                { // favorite
                    if (!arFav.ContainsKey(item.vapptitle))
                    {
                        arFav.Add(item.vapptitle, new List<string>());
                    }
                    arFav[item.vapptitle].Add(GenerateFavoriteListItem(item));
                }
                else if (item.itype == 3)
                { // saved draft
                    if (item.dcrea != null)
                    {
                        if (item.dcrea.Value.ToShortDateString() == DateTime.Today.ToShortDateString())
                        {
                            sbSavToday.Append(GenerateDraftListItem(item));
                        }
                        else if (item.dcrea.Value.ToShortDateString() == DateTime.Today.AddDays(-1).ToShortDateString())
                        {
                            sbSavYesterday.Append(GenerateDraftListItem(item));
                        }
                        else
                        {
                            sbSavOlder.Append(GenerateDraftListItem(item));
                        }
                    }
                    else
                    {
                        sbSavOlder.Append(GenerateDraftListItem(item));
                    }
                }
            }

            // Generate Favorites Block
            StringBuilder sbFavBlock = new StringBuilder();
            int cint = 0;
            foreach (var item in arFav.Keys)
            {
                cint++;
                sbFavBlock.Append("<div class=\"list-group " + (cint > 1 ? "collapsed" : "") + "\"><a class=\"group-title\" href=\"\">" + item + "</a><div class=\"group-content\" " + (cint > 1 ? "style=\"display: none;\"" : "") + " >");
                foreach (string x in arFav[item])
                {
                    sbFavBlock.Append(x);
                }
                sbFavBlock.Append("</div><div class=\"list-group\">");
            }
            homeFavoritesBlock.InnerHtml = (string.IsNullOrEmpty(sbFavBlock.ToString()) ? "No favorite form found." : sbFavBlock.ToString());

            // Saved Draft Block
            StringBuilder sbDraftBlock = new StringBuilder();
            if (sbSavToday.Length > 0)
            {
                sbDraftBlock.Append("<div class=\"list-group\"><a class=\"group-title\" href=\"\">Today</a><div class=\"group-content\" >" + sbSavToday.ToString() + "</div></div>");
            }
            if (sbSavYesterday.Length > 0)
            {
                sbDraftBlock.Append("<div class=\"list-group " + (sbSavToday.Length > 0 ? "collapsed" : "") + "\"><a class=\"group-title\" href=\"\">Yesterday</a><div class=\"group-content\" style=\"" + (sbSavToday.Length > 0 ? "display: none;" : "") + "\" >" + sbSavYesterday.ToString() + "</div></div>");
            }
            if (sbSavOlder.Length > 0)
            {
                sbDraftBlock.Append("<div class=\"list-group " + ((sbSavToday.Length > 0) || (sbSavYesterday.Length > 0) ? "collapsed" : "") + "\"><a class=\"group-title\" href=\"\">Older</a><div class=\"group-content\" style=\"" + ((sbSavToday.Length > 0) || (sbSavYesterday.Length > 0) ? "display: none;" : "") + "\" >" + sbSavOlder.ToString() + "</div></div>");
            }
            homeDraftBlock.InnerHtml = (string.IsNullOrEmpty(sbDraftBlock.ToString()) ? "No draft found." : sbDraftBlock.ToString());

        }
    }

    string GenerateDraftListItem(SPAHMIDEA_GetSavedDataResult item)
    {
        return "<a href=\"#\" class=\"list\" onclick=\"$('.main-sidebar a[data-formid=\\'"+item.vappid+"-"+item.vformid+"\\']').click(); return false;\"><div class=\"list-content\"><span class=\"list-title\">" + item.vapptitle + "</span><span class=\"list-subtitle\">" + item.vformtitle + "</span><span class=\"list-remark\">" + item.vmenupath.Replace(">", " > ") + "</span></div></a>";
    }
    string GenerateFavoriteListItem(SPAHMIDEA_GetSavedDataResult item)
    {
        return "<a href=\"#\" class=\"list\" onclick=\"$('.main-sidebar a[data-formid=\\'" + item.vappid + "-" + item.vformid + "\\']').click(); return false;\"><div class=\"list-content\"><span class=\"list-title\">" + item.vformtitle + "</span><span class=\"list-remark\">" + item.vmenupath.Replace(">", " > ") + "</span></div></a>";
    }
}